<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Init</title>
<style type="text/css">

body{
  margin:0;padding:0;font-family:'微软雅黑';overflow:hidden;
  color: #ddd;
  background: black;
}
a{text-decoration:none; color: #ddd;}
a:focus {outline: none; background: transparent;}

.dr { border: solid red 1px;}


body {
  width:1920px;
  height:1080px;
  overflow:hidden;
  background: url(bg.jpg) no-repeat;
}

.initframe {
  margin-top: 500px;
}

.initframe label {
  display: block;
  margin: 30px auto;
  text-align: center;
  font-size: 42px;
  line-height: 65px;
  letter-spacing: 5px;
  color: #888;
  width: 100%;
}

.initframe a {
  display: block;
  margin: 5px auto;
  text-align: left;
  font-size: 42px;
  line-height: 55px;
  letter-spacing: 5px;
  width: 1200px;
}

.initframe a::before {
  content: attr(label) ":";
  text-align: right;
  display: inline-block;
  width: 500px;
  font-size: 42px;
  line-height: 65px;
  padding-right: 20px;
  color: #888;
}

.initframe a:hover, .initframe a:focus {
  color: #09f;
  box-shadow: 0px 0px 6px 6px #09f;
}

.initbtn {
  margin: 30px auto;
  display: block;
  width: 1200px;
  text-align: center;
}
.initbtn a {
  display: inline-block;
  font-size: 42px;
  line-height: 75px;
  letter-spacing: 5px;
  padding: 0 60px;
  margin: 0 20px;
}
.initbtn a:hover, .initbtn a:focus {
  color: #09f;
  box-shadow: 0px 0px 6px 6px #09f;
}
  
</style>
</head>
<body>

  <div class="initframe">
    <label id="a_devid"></label>
    <a id="a_code" href="javascript:void();" onclick="return inputme(this)" label="Device Code">--------</a>
    <a id="a_contact" href="javascript:void();" onclick="return inputme(this)" label="Contact Info">-----</a>
    <a id="a_tel" href="javascript:void();" onclick="return inputme(this)" label="Phone Number">------</a>
  </div>
  <div class="initbtn">
    <a href="javascript:activate();">Initialize</a>
<!--     <a href="javascript:loadFromUSB();">Load Code</a>
 -->    <a href="javascript:goLocal();">More ...</a>
  </div>

</body>
</html>
<script type="text/javascript" src="lang.js"></script>
<script type="text/javascript" src="jsget.js"></script>
<script type="text/javascript" src="sdeviceinfo.js"></script>
<script type="text/javascript">
var _debug = false;


var sdeviceinfo = new SimpleDeviceInfo();

function setDeviceID() {
    if ( typeof( wae ) == "undefined" ) {
      setTimeout( function() {
        setDeviceID();
      }, 1000 );
      return;
    }

    document.getElementById("a_devid").innerHTML = TR("Device ID") + ":" + sdeviceinfo.getID();
}


function inputme( ele ) {
  setTimeout( function(){
    var res = prompt( TR("Please input") + TR( ele.getAttribute("label") ));
    ele.innerHTML = res.replace( /\s+/g, "");    
  } , 500 );
  return false;
}

function loadFromUSB() {
    var file_obj = wae.create("DATA.File");
    var mlines = file_obj.readAll("/proc/mounts").split(/[\n\r]+/);
    var licinfo = {};

    for( var i = 0 ; i< mlines.length; i++ ) {
        var paths = mlines[i].split(/\s+/);
        if ( ! paths[1] ) continue;
        if ( paths[0].indexOf("/dev") != 0 ) continue;
        if ( paths[1].indexOf("/", 2) <= 0 ) continue;
        if ( ! file_obj.isDir( paths[1] ) ) continue;

        var vpath = paths[1];
        if( _debug ) console.log("Check USB Root: "  + vpath + " : " + mlines[i]);
        var usbdatainfofile = "tvata.license";
        var res = file_obj.readAll( vpath + "/" + usbdatainfofile );
        if( ! res ) continue;
        var resline = res.split( /[\r\n]+/ );
        for( var j = 0; j<resline.length; j++ ) {
          var kv = resline[j].split( /[:= \t]+/, 2 );
          if( !kv || !kv[0] || !kv[1] ) continue;
          licinfo[ kv[0] ] = kv[1];
        }
        if(_debug) console.log("reload usbinfo: " + JSON.stringify(licinfo) );
    }
    document.getElementById("a_code").innerHTML = licinfo["code"] ? licinfo["code"] : "";
    document.getElementById("a_contact").innerHTML = licinfo["contact"] ? licinfo["contact"] : "";
    document.getElementById("a_tel").innerHTML = licinfo["tel"] ? licinfo["tel"] : "";
}

function activate() {
    var code = document.getElementById("a_code").innerHTML;
    var contact = document.getElementById("a_contact").innerHTML;
    var tel = document.getElementById("a_tel").innerHTML ;

    var res = sdeviceinfo.activate( code, contact, tel);
    if ( res == "OK" || res.indexOf("license_exist") >= 0 ) {
      var dinfo = sdeviceinfo.getinfo();

      if ( !!dinfo && !!dinfo.bo_url ) {
        var server_url = dinfo.bo_url.replace( "index.php", "");
        var dobj = wae.create("DATA.Parameters");
        dobj.addContentProviderPersistenceWithAutoCommit("mainhosturl", server_url );

        alert( TR("Device initialze Successed!") + "\n" + TR("System will restart now!"));
        var app = wae.create("Application");
        app.restartApplication();
        return;        
      }
    }

    if( _debug ) console.log("activated  error : " + res );
    var r = confirm( TR("Activated Failed! Retry?") + "\n\nError Message:\n" + res );
    if( r ) {
      setTimeout( function() { activate(); }, 1000 );
    } else {
      location.replace( "init0.html" );
    }
}

function goLocal() {
  var intent = wae.create("Intent");
  intent.startActivity("", "com.vatata.wae.localapp.LocalAppActivity");
}

function changeLabelLang() {
  var eles = document.getElementsByTagName("a");
  for( var i = 0; i<eles.length; i++ ) {
    var l = eles[i].getAttribute("label");
    if ( l != null )
      eles[i].setAttribute( "label", TR( l ));
    else
      eles[i].innerHTML = TR( eles[i].innerHTML );
  }
}


changeLabelLang();

window.onWAEReady = function() {
  setDeviceID();
  loadFromUSB();
}

</script>
<script type="text/javascript" src="key.js"></script>


